﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL = AccessDAL;

namespace BLL
{
    public class Master
    {
        DAL.MasterTableAdapters.MasterTableAdapter DM =
            new DAL.MasterTableAdapters.MasterTableAdapter();

        MODEL.Master _MM = new MODEL.Master();


        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="MM"></param>
        /// <returns></returns>
        public MODEL.Master GetDataByID(MODEL.Master MM)
        {
            try
            {
                DataTable DT = DM.GetDataByID(MM.Master_ID);

                _MM.Master_ID = Convert.ToInt32(DT.Rows[0]["Master_ID"]);
                _MM.Master_IsDel = Convert.ToBoolean(DT.Rows[0]["Master_IsDel"]);
                _MM.Master_IsLock = Convert.ToBoolean(DT.Rows[0]["Master_IsLock"]);
                _MM.Master_LastIP = DT.Rows[0]["Master_LastIP"].ToString();
                _MM.Master_LastTime = Convert.ToDateTime(DT.Rows[0]["Master_LastTime"]);
                _MM.Master_LoginID = DT.Rows[0]["Master_LoginID"].ToString();
                _MM.Master_Password = DT.Rows[0]["Master_Password"].ToString();
                _MM.Master_RealName = DT.Rows[0]["Master_RealName"].ToString();
                _MM.Master_Role = Convert.ToInt32(DT.Rows[0]["Master_Role"]);

                return _MM;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加管理员
        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="MM"></param>
        public void Add(MODEL.Master MM)
        {
            try
            {
                DM.Add(MM.Master_LoginID, MM.Master_Password, MM.Master_LastTime, MM.Master_LastIP, MM.Master_Role,
                    MM.Master_RealName, MM.Master_IsLock, MM.Master_IsDel);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改密码
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="MM"></param>
        public void ModPass(MODEL.Master MM)
        {
            try
            {
                _MM = GetDataByID(MM);
                string OldPass = _MM.Master_Password;
                if (OldPass != MM.Master_Password)
                {
                    throw new Exception("原始密码验证失败");
                }

                DM.ModPass(MM.Master_NewPassword, MM.Master_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除管理员
        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="MM"></param>
        public void Del(MODEL.Master MM)
        {
            try
            {
                _MM = GetDataByID(MM);
                int DelRole = _MM.Master_Role;
                if (MM.Master_Role > DelRole)
                {
                    throw new Exception("您无法删除比您更高级别的管理员");
                }

                DM.Del(MM.Master_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 管理员登陆
        /// <summary>
        /// 管理员登陆
        /// </summary>
        /// <param name="MM"></param>
        public DataTable MasterLogin(MODEL.Master MM)
        {
            try
            {
                DataTable DT_Login = DM.GetDataForLogin(MM.Master_LoginID, MM.Master_Password);

                if (DT_Login.Rows[0]["Master_IsLock"].ToString() == "False")
                {
                    throw new Exception("此账户已被锁定");
                }
                if (DT_Login.Rows[0]["Master_IsDel"].ToString() == "True")
                {
                    throw new Exception("此账户已被删除");
                }

                if (DT_Login.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    int MasterID = Convert.ToInt32(DT_Login.Rows[0]["Master_ID"]);

                    return DT_Login;
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "在位置 0 处没有任何行。")
                {
                    throw new Exception("用户名或密码错误");
                }
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        #endregion


        #region 修改管理员登陆IP和登录时间
        /// <summary>
        /// 修改管理员登陆IP和登录时间
        /// </summary>
        /// <param name="MM"></param>
        public void ModMasterLoginIPAndDateTime(MODEL.Master MM)
        {
            try
            {
                DM.ModMasterLoginIP_DateTime(MM.Master_LastTime, MM.Master_LastIP, MM.Master_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

    }
}
